package com.ty.tour.common;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * mybatis-plus 逆向生成工具
 */

public class MybatisPlusGenerator {
    private static MybatisPlusGenerator single = null;

    private MybatisPlusGenerator() {
        super();
    }

    private static MybatisPlusGenerator getSingle() {
        if (single == null) {
            single = new MybatisPlusGenerator();
        }
        return single;
    }

//    public void autoGeneration() {
//        GlobalConfig config = new GlobalConfig();
//        String dbUrl = "jdbc:mysql://120.78.128.99:3306/foxcall?useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true";
//        DataSourceConfig dataSourceConfig = new DataSourceConfig();
//        dataSourceConfig.setDbType(DbType.MYSQL)
//                .setUrl(dbUrl)
//                .setUsername("root")
//                .setPassword("zkzy123@...!!")
//                .setDriverName("com.mysql.jdbc.Driver");
//        StrategyConfig strategyConfig = new StrategyConfig();
//        strategyConfig
//                .setCapitalMode(true)
//                .setEntityLombokModel(true)
//                .setDbColumnUnderline(true)
//                .setNaming(NamingStrategy.underline_to_camel)
//                .setInclude(new String[]{"new_table"}); // 输入需要生成的表 *********-----------------------------------------
//
//        config.setActiveRecord(false)
//                .setEnableCache(false)
//                .setAuthor("Author is ***")
//                //指定输出文件夹位置
//                .setOutputDir(".")
//                .setFileOverride(true)
//                .setMapperName("%sMapper")
//                .setXmlName("%sMapper")
//                .setControllerName("%sController")
//                .setServiceName("%sService")
//                ;
//        new AutoGenerator().setGlobalConfig(config)
//                .setDataSource(dataSourceConfig)
//                .setStrategy(strategyConfig)
//                .setPackageInfo(
//                        new PackageConfig()
//                                .setParent(".")
//                                .setController("com-call-api.src.main.java.com.call.Controller")
//                                .setEntity("com-call-pojo.src.main.java.com.call.pojo")
//                                .setXml("com-call-api.src.main.resources.mapper")
//                                .setMapper("com-call-mapper.src.main.java.com.call.mapper")
//                                .setService("com-call-service.src.main.java.com.call.service")
//                                .setServiceImpl("com-call-service.src.main.java.com.call.service.imp")
//                ).execute();
//    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
//        MybatisPlusGenerator generator = MybatisPlusGenerator.getSingle();
//        generator.autoGeneration();

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(".")
                .setAuthor("Author is ***")
                .setOpen(false)
                .setFileOverride(true)
                .setEnableCache(false).setMapperName("%sMapper")
                .setXmlName("%sMapper").setControllerName("%sController").setServiceName("%sService");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost/tourism?useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc
                .setParent(".")
                .setController("com.ty.tour.controller")
                .setEntity("com.ty.tour.pojo")
                .setXml("resources.mapper")
                .setMapper("com.ty.tour.mapper")
                .setService("com.ty.tour.service")
                .setServiceImpl("com.ty.tour.service.serviceImp");
        mpg.setPackageInfo(pc);


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude(new String[]{"attractions","custom","order","product","user_info"});
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setCapitalMode(true);
        strategy.setEntityLombokModel(true);
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();


    }

}
